Safe C++ by Vladimir Kushnir
Author:Vladimir Kushnir [Vladimir Kushnir]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / C++
ISBN: 9781449321352
Publisher: O'Reilly Media
Published: 2012-05-29T16:00:00+00:00
Provide both methods.
There is also an opposite situation when the SomeClass::Find() method returns a pointer to an object but the user does not have ownership of it:
// Returns a pointer to a result, caller DOES NOT OWN the result. MyClass* SomeClass::Find(const Inputs& inputs);
In this case, the pointer returned by this function points to an object that belongs to something inside the SomeClass object.
The first problem here is that the SomeClass object thinks that it is responsible for deleting the MyClass instance to which it just returned a pointer, and therefore it will delete it at some point in the future. In this case, if the user of this function will delete the pointer he received, this instance will be deleted more than once, which is not a good idea. Second, this instance might be part of an array of MyClass objects that is created inside, say, a template vector using operator new[] (with brackets), and we are now trying to delete an object from that array using operator delete without brackets. This is also not good. Finally, the instance of MyClass could be created on stack, and should not ever be deleted using operator delete at all.
In this case, any attempt to delete this object that we do not own—directly or by assigning it to a smart pointer of any kind that would take ownership of it—would lead to disaster. An appropriate way of returning this pointer is to return a “semi-smart” pointer that does not own the object it points to. This will be discussed in the next chapter.
Rules for this chapter to avoid memory leaks:
Every time you create an object using the new operator, immediately assign the result to a smart pointer (reference counting point or scoped pointer is recommended).
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(11975)
Hello! Python by Anthony Briggs(9423)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9388)
The Mikado Method by Ola Ellnestam Daniel Brolund(9352)
Dependency Injection in .NET by Mark Seemann(8896)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7895)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7413)
Grails in Action by Glen Smith Peter Ledbrook(7332)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7161)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6006)
Kotlin in Action by Dmitry Jemerov(4683)
Practical Vim (for Kathryn Amaral) by Drew Neil(3746)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(3625)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3532)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3277)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(3188)
Microservices with Go by Alexander Shuiskov(2973)
Practical Design Patterns for Java Developers by Miroslav Wengner(2891)
Learning Java by Patrick Niemeyer & Daniel Leuck(2890)
![](/ebook_detail_files/space.gif)